package com.ningxia.water.service.impl;

import com.ningxia.water.entity.TsMonthData;
import com.ningxia.water.mapper.TsMonthDataMapper;
import com.ningxia.water.mapper.ZtCityInfoMapper;
import com.ningxia.water.mapper.ZtDistrictInfoMapper;
import com.ningxia.water.service.ITsMonthDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningxia.water.vo.out.MonthConsumedVo;
import com.ningxia.water.vo.out.MonthReportVo;
import com.ningxia.water.vo.out.OtherReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hz
 * @since 2021-03-15
 */
@Service
public class TsMonthDataServiceImpl extends ServiceImpl<TsMonthDataMapper, TsMonthData> implements ITsMonthDataService {
    @Autowired
    private TsMonthDataMapper monthDataMapper;

    @Autowired
    private ZtDistrictInfoMapper districtInfoMapper;

    @Autowired
    private ZtCityInfoMapper cityInfoMapper;

    /**
     * 此机井的每个月的用水量
     *
     * @param wellId
     * @param year
     * @return
     */
    @Override
    public List<MonthConsumedVo> getMonthConsumedByWellId(Integer wellId, String year) {
        String startTime = year + "-01";
        String endTime = year + "-12";
        List<MonthConsumedVo> list = monthDataMapper.getMonthConsumedByWellId(wellId, startTime, endTime);
        ListIterator<MonthConsumedVo> iterator = list.listIterator();
        while (iterator.hasNext()) {
            MonthConsumedVo next = iterator.next();
            next.setMonth(next.getMonth().split("-")[1]);
        }
        return list;
    }

    @Override
    public List<MonthReportVo> getAllInfos() {

        return null;
    }

    /**
     * 传过来的是什么？
     * 1.月份 2.地址类型 3.地址id
     *
     * @param month    月份
     * @param zoneType 地址类型
     * @param zoneId   地址id（工业园区id或乡镇id）
     * @return
     */
    @Override
    public List<MonthReportVo> getApplicantsInfos(String month, Integer zoneType, Integer zoneId) {
        //该地区下的所有用户及其许可用水量和计划用水量
        List<MonthReportVo> applicantsInfos = monthDataMapper.getApplicantsInfos(month.split("-")[0], zoneType, zoneId);
        //该地区下的累计用水量、各种用途用水量
        List<MonthReportVo> volumeInfos = monthDataMapper.getVolumeInfos(month, zoneType, zoneId);


        //合并
        //1.根据applicantId将许可水量和计划水量进行划分
        Map<Integer, MonthReportVo> map = applicantsInfos.stream()
                .collect(Collectors.toMap(MonthReportVo::getApplicantId, m -> m));
        //2.将两者合并
        volumeInfos.forEach(n -> {
            if (map.containsKey(n.getApplicantId())) {
                MonthReportVo temp = map.get(n.getApplicantId());
                //复制过去
                n.setLicenseVolume(temp.getLicenseVolume());
                n.setPlanVolume(temp.getPlanVolume());
            }
        });

        return volumeInfos;
    }

    /**
     * 获取某区县下所有乡镇综合统计报表
     * 获取某个工业园区下所有用户综合统计报表
     *
     * @param month
     * @param zoneType
     * @param IdList
     * @return
     */
    @Override
    public List<OtherReportVo> getTownOrAreaInfos(String month, int zoneType, List<Integer> IdList) {
        List<OtherReportVo> townInfos = new ArrayList<>();
        for (Integer id : IdList) {
            List<MonthReportVo> infos = this.getApplicantsInfos(month, zoneType, id);
            townInfos.add(new OtherReportVo(
                    id,
                    infos.stream().mapToDouble(MonthReportVo::getLicenseVolume).sum(),
                    infos.stream().mapToDouble(MonthReportVo::getPlanVolume).sum(),
                    infos.stream().mapToDouble(MonthReportVo::getTotalVolume).sum(),
                    infos.stream().mapToDouble(MonthReportVo::getIndustryVolume).sum(),
                    infos.stream().mapToDouble(MonthReportVo::getAgricultureVolume).sum(),
                    infos.stream().mapToDouble(MonthReportVo::getLifeVolume).sum(),
                    infos.stream().mapToDouble(MonthReportVo::getProductVolume).sum()
            ));
        }

        return townInfos;
    }

    /**
     * 具体月份某市下的所有区县的综合统计数据
     *
     * @param month
     * @param districtIdList
     * @return
     */
    @Override
    public List<OtherReportVo> getDistrictInfos(String month, List<Integer> districtIdList) {
        List<OtherReportVo> districtInfos = new ArrayList<>();
        for (Integer id : districtIdList) {
            //获取该区县下的所有乡镇idList
            List<Integer> idList = districtInfoMapper.getAllTownIds(id);
            //每个乡镇的综合数据
            List<OtherReportVo> infos = this.getTownOrAreaInfos(month, 2, idList);
            districtInfos.add(new OtherReportVo(
                    id,
                    infos.stream().mapToDouble(OtherReportVo::getLicenseVolume).sum(),
                    infos.stream().mapToDouble(OtherReportVo::getPlanVolume).sum(),
                    infos.stream().mapToDouble(OtherReportVo::getTotalVolume).sum(),
                    infos.stream().mapToDouble(OtherReportVo::getIndustryVolume).sum(),
                    infos.stream().mapToDouble(OtherReportVo::getAgricultureVolume).sum(),
                    infos.stream().mapToDouble(OtherReportVo::getLifeVolume).sum(),
                    infos.stream().mapToDouble(OtherReportVo::getProductVolume).sum()
            ));
        }
        return districtInfos;
    }

    @Override
    public List<OtherReportVo> getCityInfos(String month, List<Integer> cityList) {
        List<OtherReportVo> cityInfos = new ArrayList<>();
        for (Integer id : cityList) {
            //获取该区县下的所有乡镇idList 和 工业园区idList
            List<Integer> districtIdList = cityInfoMapper.getAllDistrictIds(id);
            List<Integer> areaIdList = cityInfoMapper.getAllAreaIds(id);
            //每个区县的综合数据
            List<OtherReportVo> districtInfos = this.getDistrictInfos(month, districtIdList);
            List<OtherReportVo> areaInfos = this.getTownOrAreaInfos(month, 1, areaIdList);
            cityInfos.add(new OtherReportVo(
                    id,
                    districtInfos.stream().mapToDouble(OtherReportVo::getLicenseVolume).sum()
                            + areaInfos.stream().mapToDouble(OtherReportVo::getLicenseVolume).sum(),
                    districtInfos.stream().mapToDouble(OtherReportVo::getPlanVolume).sum()
                            + areaInfos.stream().mapToDouble(OtherReportVo::getPlanVolume).sum(),
                    districtInfos.stream().mapToDouble(OtherReportVo::getTotalVolume).sum()
                            + areaInfos.stream().mapToDouble(OtherReportVo::getTotalVolume).sum(),
                    districtInfos.stream().mapToDouble(OtherReportVo::getIndustryVolume).sum()
                            + areaInfos.stream().mapToDouble(OtherReportVo::getIndustryVolume).sum(),
                    districtInfos.stream().mapToDouble(OtherReportVo::getAgricultureVolume).sum()
                            + areaInfos.stream().mapToDouble(OtherReportVo::getAgricultureVolume).sum(),
                    districtInfos.stream().mapToDouble(OtherReportVo::getLifeVolume).sum()
                            + areaInfos.stream().mapToDouble(OtherReportVo::getLifeVolume).sum(),
                    districtInfos.stream().mapToDouble(OtherReportVo::getProductVolume).sum()
                            + areaInfos.stream().mapToDouble(OtherReportVo::getProductVolume).sum()
            ));
        }
        return cityInfos;
    }


}
